﻿//Force setting jQuery support cross domain.
//This line mostly has effective in IE
jQuery.support.cors = true;

$(document).ready(function () {

    if (readCookie("userId") != '') {
        $('#box_signin').addClass('hide');
        $('#box_welcome').removeClass('hide');
        getInfoUser(readCookie("userId"));
    }
    function checkLogin() { //validate form
        var user = $('#txtUsername').val();
        var pass = $('#txtPassword').val();
        if (user == '' || user == 'Username') {
            if (pass == '') {
                $('#title_pass').css('color', '#ff5050');
            }
            //$('#warning').html('Please complete the required fields');
            $('#title_user').css('color', '#ff5050');
            $('#txtUsername').val('');
            $('#txtUsername').focus();
            //$('#warning').css('display', 'block');
            return false;
        }
        if (pass == '') {
            //$('#warning').html('Please complete the required fields');
            $('#title_pass').css('color', '#ff5050');
            $('#txtPassword').val('');
            $('#txtPassword').focus();
            //$('#warning').css('display', 'block');
            return false;
        }
        return true;
    }
    $('#btn_logout').click(function () {
        writeCookie("userId", "", 1);
        writeCookie("RoleType", "", 1);
        writeCookie("CampId", "", 1);
        $('#box_signin').removeClass('hide');
        $('#box_welcome').addClass('hide');
        var url = location.href;
        var returnURL = "";
        var arrURL = url.split('/');
        if (arrURL[3].toUpperCase() == 'GEM') {
            if (arrURL.length > 4) {
                if (arrURL[4].toUpperCase() == 'PROFILE') {
                    window.location.replace('../Profile/Login');
                }
                else if (arrURL[4].toUpperCase() == 'CORPORATE') {
                    window.location.replace('../' + arrURL[4] + '/' + arrURL[5]);
                }
                else {
                    window.location.replace('../' + arrURL[4] + '');
                }
            }
            else {
                window.location.replace('../');
            }
        }
        else {
            if (arrURL[3].toUpperCase() == 'PROFILE') {
                window.location.replace('../' + arrURL[3] + '/Login');
            }
            else if (arrURL[3].toUpperCase() == 'CORPORATE') {
                window.location.replace('../' + arrURL[3] + '/' + arrURL[4]);
            }
            else {
                window.location.replace('../' + arrURL[3] + '');
            }
        }
    });
    $('#btn_sigin').click(function () {
        if (checkLogin()) {
            $.ajax({
                url: gb_servicePath + "/authenticateduser/login.json",
                crossDomain: "true",
                type: "POST",
                contentType: 'application/json; charset=utf-8',
                traditional: true,
                data: JSON.stringify(buildLoginTop()),
                "dataType": 'json',
                success: function (data) {
                    var admin = data.ReturnData;
                    writeCookie("userId", data.ReturnData.AuthenticatedUserID, 1);
                    getInfoUser(data.ReturnData.AuthenticatedUserID);
                    $('#box_signin').addClass('hide');
                    $('#signin_cotent').css('display', 'none');
                    $('#box_welcome').removeClass('hide');
                    $('#navAccountLink').removeClass('openToggler');
                    $('#hello .navigation').css('display', 'none');
                    var path_name = location.pathname;
                    if (path_name.indexOf('Corporate') != -1) {
 
                        if (admin.AuthenticatedUserID == "2") {
                            window.location.replace(profileAdmin);
                        }
                        if (admin.AuthenticatedUserID == "1") {
                            window.location.replace(profile);
                        }
                        if (admin.AuthenticatedUserID == "3" || admin.UserRoles[0].RoleType == "4") {
                            window.location.replace(Admin);
                        }
                    }
                    else if (path_name.indexOf('Profile') != -1) {
                        if (location.search == "") {
                            if (admin.AuthenticatedUserID == "2") {
                                window.location.replace(profileAdmin);
                            }
                            if (admin.AuthenticatedUserID == "1") {
                                window.location.replace(profile);
                            }
                            if (admin.AuthenticatedUserID == "3" || admin.UserRoles[0].RoleType == "4") {
                                window.location.replace(Admin);
                            }
                        }
                        else {
                            window.location.replace('../' + getURLback('ref'));
                        }
                    }
                },
                error: function (jqXHR, textStatus) {
                    $('#title_user').css('color', '#ff5050');
                    $('#title_pass').css('color', '#ff5050');
                    //$('#warning').html('User name or password invalid, please try again');
                    //$('#warning').css('display', 'block');
                    if (isBug) {
                        alert("Error in login");
                    }
                }
            });
        }
        else {
            return false;
        }
    });
    $('#txtUsername').keypress(function () {
        $('#title_user').css('color', '#ffffff');
        $('#title_pass').css('color', '#ffffff');
    });
    $('#txtPassword').keypress(function () {
        $('#title_user').css('color', '#ffffff');
        $('#title_pass').css('color', '#ffffff');
    });
    function getInfoUser(id) {
        // Get Info User Login
        $.ajax({
            url: gb_servicePath + '/authenticateduser/ID/' + id + '.json',
            crossDomain: "true",
            type: "GET",
            contentType: 'application/json; charset=utf-8',
            traditional: true,
            data: "",
            dataType: 'json',
            success: function (data) {
                if (data.StatusCode == 200) {
                    var admin = data.ReturnData;
                    writeCookie("RoleType", admin.UserRoles[0].RoleType, 1);
                    if (admin.CampaignRoleCountryInfo != "") {
                        writeCookie("CampId", admin.CampaignRoleCountryInfo[0].CampaignRoleInfo.CampaignInfo.Id, 1);
                    }
                    else {
                        writeCookie("CampId", "0", 1);
                    }
                    $('#hello b').html(admin.FirstName + ' ' + admin.LastName);
                    if (admin.UserRoles[0].RoleType == "2") {
                        $('#link_Profile').attr('href', profileAdmin);
                    }
                    if (admin.UserRoles[0].RoleType == "1") {
                        $('#link_Profile').attr('href', profile);
                    }
                    if (admin.UserRoles[0].RoleType == "3") {
                        $('#link_Profile').attr('href', Admin);
                    }
                }
            },
            error: function (jqXHR, textStatus, xerror) {
                if (isBug) {
                    alert("Error in authenticateduser/ID.json ->" + xerror);
                }
            }
        });
    }
});
function buildLoginTop() {
    var Login = new Object();
    Login.username = $('#txtUsername').val();
    Login.password = $('#txtPassword').val();
    return Login;
}